#include<iostream>
using namespace std;

struct ListNode {
	int val;
	ListNode* next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
public:
	ListNode* removeElements(ListNode* head, int val)
	{
		ListNode Dummy(0);
		Dummy.next = head;
		ListNode* cur = &Dummy;
		while (cur->next)
		{
			if (cur->val == val)
			{
				ListNode* toDelete = cur;
				cur->next = cur->next->next;
				delete toDelete;
			}
			else
			cur = cur->next;
		}
		return Dummy.next;
	}
};

int main()
{

	ListNode* head1 = new ListNode(1);
	ListNode* head2 = new ListNode(2);
	ListNode* head3 = new ListNode(6);
	ListNode* head4 = new ListNode(3);
	ListNode* head5 = new ListNode(4);
	ListNode* head6 = new ListNode(5);
	ListNode* head7 = new ListNode(6);

	head1->next = head2;
	head2->next = head3;
	head3->next = head4;
	head4->next = head5;
	head5->next = head6;
	head6->next = head7;
	Solution s;

	s.removeElements(head1, 6);
	return 0;
}